package com.me.web;

import com.me.dao.impl.PetDao;
import com.me.dao.impl.SpecialityDao;
import com.me.dao.impl.VetDao;
import com.me.domain.Speciality;
import com.me.domain.Vet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

import java.util.List;

@WebServlet("/vet")
public class VetServlet extends BaseServlet {
    @Override
    protected void doGet() throws ServletException, IOException {
        String m = getParam("m");
        if (m == null) {
            getAll();
        }else if (m.equals("toAdd")){
            toAdd();
        }else if (m.equals("edit")) {
            edit();
        }
    }

    private void edit() throws ServletException, IOException {
        VetDao vetDao = new VetDao();
        Vet vet = vetDao.getById(getIntParam("id"));
        setReqAttr("vet",vet);
        SpecialityDao specDao = new SpecialityDao();
        setReqAttr("specs",specDao.getAll());
        forward("/admin/vetEdit.jsp");
    }

    private void getAll() throws ServletException, IOException{
        VetDao vetDao = new VetDao();
        List<Vet> vets = vetDao.getAll();
        setReqAttr("vets",vets);
        forward("/admin/vetmanage.jsp");
    }

    private void toAdd() throws ServletException, IOException {
        SpecialityDao specDao = new SpecialityDao();
        setReqAttr("specs",specDao.getAll());
        forward("/admin/vetadd.jsp");
    }

    @Override
    protected void doPost() throws ServletException, IOException {
        String m = getParam("m");
        if (m == null) {
            getAll();
        }
        else if ("add".equals(m)) {
            add();
        }else if ("search".equals(m)){
            search();
        } else if ("edit".equals(m)) {
            update();
        }else if ("delete".equals(m)){
            delete();
        }
    }

    private void delete() throws ServletException, IOException{
        Vet vet = new Vet();
        vet.setId(getIntParam("id"));
        VetDao vetDao = new VetDao();
        vetDao.delete(vet);
        setReqAttr("msg","删除成功");
        forward("./vet");
    }

    private void update() throws ServletException, IOException {
        Vet vet = new Vet();
        vet.setName(getParam("name"));
        vet.setId(getIntParam("id"));

        String specIDs[] = getParamVals("specID");
        if (specIDs == null||specIDs.length==0){
            setReqAttr("err","请选择至少一项专业特长");
        }

        for (int i = 0;i<specIDs.length;i++){
            Speciality spec = new Speciality();
            spec.setId(Integer.valueOf(specIDs[i]));
            vet.getSpecs().add(spec);
        }

        VetDao vetDao = new VetDao();
        vetDao.update(vet);
        setReqAttr("msg","更改信息成功");
        forward("./vet");
    }

    private void search() throws ServletException, IOException {
        String vetName = getParam("vetName");
        String specName = getParam("specName");
        VetDao vetDao = new VetDao();
        List<Vet> vets = vetDao.search(vetName,specName);

        if (vets.size()==0){
            setReqAttr("msg","没有找到相关医生信息");
            forward("/vetsearch.jsp");
        }else {
            setReqAttr("vets",vets);
            forward("/vetsearch_result.jsp");
        }
    }

    private void add() throws ServletException, IOException {
        String vetName = getParam("name");
        if (vetName == null||"".equals(vetName)){
            setReqAttr("err","请输入医生姓名");
        }
        String specIDs[] = getParamVals("specID");
        if (specIDs == null||specIDs.length==0){
            setReqAttr("err","请选择至少一项专业特长");
        }
        Vet vet = new Vet();
        vet.setName(getParam("name"));


        for (int i = 0;i<specIDs.length;i++){
         Speciality spec = new Speciality();
         spec.setId(Integer.valueOf(specIDs[i]));
         vet.getSpecs().add(spec);
        }
        VetDao vetDao = new VetDao();
        vetDao.save(vet);
        setReqAttr("msg","操作成功");
        forward("./vet");

    }

}
